package com.night.service;

import com.alibaba.fastjson.JSONObject;
import com.night.util.MD5Util;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HtmlUtil {
    public static void main(String[] args) throws Exception {
        testGet();
    }

    public static void testGet() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet("http://www.sina.com.cn/");
        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");

        CloseableHttpResponse response = httpclient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            String responseBody = EntityUtils.toString(entity, "UTF-8");
            System.out.println(responseBody);
            EntityUtils.consume(entity);

            // 解析网页源代码
            Document document = Jsoup.parse(responseBody);
            // 获取所有图片的地址
            Elements elements = document.getElementsByTag("img");

            for (Element element : elements) {
                String imgSrc = element.attr("src");
                if (!"".equals(imgSrc) && (imgSrc.startsWith("http://") || imgSrc.startsWith("https://"))) {
                    // 判断imgSrc是否为空且是否以"http://"开头
                    System.out.println("正在下载的图片的地址：" + imgSrc);
                }
            }
        }
        response.close();
        httpclient.close();
    }


    public static void testHttpClientA() throws IOException {
        //使用默认配置的httpclient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //即将访问的url
        String url = "https://www.192tt.com";
        //get形式的访问
        HttpGet httpGet = new HttpGet(url);

        //执行请求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            //打印请求的状态码  请求成功为200
            System.out.println(response.getStatusLine().getStatusCode());
            //打印请求的实体内容 返回json格式
            HttpEntity entity = response.getEntity();
            //获取所有头信息
            Header[] allHeaders = response.getAllHeaders();
            for (Header allHeader : allHeaders) {
                System.out.println(allHeader.getName());
                System.out.println(allHeader.getValue());
                System.out.println(allHeader.toString());
            }

            //方法一 官方不推荐
//            if (entity != null) {
//                //输出更详细的抓取内容(html格式)
//                System.out.println(EntityUtils.toString(entity, "utf-8"));
//            }
            //释放资源
            EntityUtils.consume(entity);
            //方法二 官方推荐 使用流的形式处理请求结果
            if (entity != null) {
                InputStream content = entity.getContent();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
                bufferedReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            response.close();
        }
    }


}
